<?php
class Helper_ModelLoader extends Zend_Controller_Action_Helper_Abstract
{   
	private $_ctrl;
	public function setCtrl($ctrl){
		$this->_ctrl = $ctrl;
	}
	
	public function getEventInfo($eid, $use_cached=true, $sts=true)
    {
    	$eid = (int)$eid;
    	if(!$eid)
    		return false;
    		
    	static $s_events;
    	if($use_cached && isset($s_events[$eid]) && $sts)
    		return $s_events[$eid];
    	else 	unset($s_events[$eid]);
    	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
		$cache_key="info_$eid";
		
		if ($use_cached){
			$event=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
			if ($event)
			{
				$s_events[$eid] = $event;
				return $event;
			}
		}
		
		$eventModel=AppGlobal::loadModel("Event", 'event');
		
		$event=$eventModel->getDetail($eid);
		//if ($event){
		$ctrl->setCache($cache_key, $event, EVENT_CACHE_PREFIX);
		//}
		$s_events[$eid] = $event;
		
		return $event;
	}
	
	public function getEventInfoByShortcut($shortcut, $use_cached=true, $sts=true){
		
    	if(!$shortcut)
    		return false;
    		
    	static $s_events_byshortcut;
    	if($use_cached && isset($s_events_byshortcut[$shortcut]) && $sts)
    		return $s_events_byshortcut[$shortcut];
    	else 	unset($s_events_byshortcut[$shortcut]);
    	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
		$cache_key="info_byshortcut_$shortcut";
		
		if ($use_cached){
			$event=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
			if ($event)
			{
				$s_events_byshortcut[$shortcut] = $event;
				return $event;
			}
		}
		
		$eventModel=AppGlobal::loadModel("Event", 'event');
		
		$event=$eventModel->getDetailByShortcut($shortcut);
		//if ($event){
		$ctrl->setCache($cache_key, $event, EVENT_CACHE_PREFIX);
		//}
		$s_events_byshortcut[$shortcut] = $event;
		
		return $event;
	}
	
	public function changeEventInfo($eid, $newVals=null){
		$eid = (int)$eid;
		$cache_key="info_$eid";
			
		$ctrl=$this->_actionController;
		$event = $ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
		if(is_array($newVals) && count($newVals)>0){

			foreach($newVals as $key=>$val){
				if($key != 'eid')
					$event->$key = $val;
			}
			if(isset($newVals['eid']))
				unset($newVals['eid']);
			$ctrl->loadModel('Event')->update($newVals, 'eid='.$eid);
			$ctrl->setCache($cache_key, $event, EVENT_CACHE_PREFIX);
			
			static $s_events, $s_events_byshortcut;
			unset($s_events[$eid]);
			if($event->short_cut){
				unset($s_events_byshortcut[$event->short_cut]);
			}
		}
		return $event;
	}
	
	public function getMemberList($eid, $use_cached=true, $static=true)
    {
    	$eid = (int)$eid;
    	if(!$eid)
    		return false;
    		
    	static $_s_members;
    	if($use_cached && isset($_s_members[$eid]) && $static)
    		return $_s_members[$eid];
    	else 	unset($_s_members[$eid]);
    	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
			
		$cache_key="member_list_$eid";
		
		
		if ($use_cached)
		{
			$memberList=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
			if ($memberList !== false){
				$_s_members[$eid] = $memberList;
				return $memberList;
			}
		}
    	$eventModel = AppGlobal::loadModel("Member", 'event');
			
		$memberList = $eventModel->getList($eid);
		if ($use_cached && $memberList !== false){
			$_s_members[$eid] = $memberList;
			$ctrl->setCache($cache_key, $memberList, EVENT_CACHE_PREFIX);
		}
		
		return $memberList;
	}
	
	public function getMemberStatus($eid, $uid, $use_cached=true, $static=true)
    {
    	$eid = (int)$eid;
    	if(!$eid)
    		return false;
    	$uid = (int)$uid;
    	if(!$uid)
    		return false;
    		
    	static $_s_member_status;
    	if($use_cached && isset($_s_member_status[$eid]) && isset($_s_member_status[$eid][$uid]) && $static)
    		return $_s_member_status[$eid][$uid];
    	else 	unset($_s_member_status[$eid][$uid]);
    	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
			
		$cache_key="member_status_".$eid."_".$uid;
		
		
		if ($use_cached)
		{
			$memberStatus=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
			if ($memberStatus !== false){
				$_s_member_status[$eid][$uid] = $memberStatus;
				return $memberStatus;
			}
		}
    	$eventModel = AppGlobal::loadModel("Member", 'event');
		
		$memberStatus = $eventModel->getMemberStatus($eid, $uid);
		if ($use_cached && $memberStatus !== false){
			$_s_member_status[$eid][$uid] = $memberStatus;
			$ctrl->setCache($cache_key, $memberStatus, EVENT_CACHE_PREFIX);
		}
		
		return $memberStatus;
	}
	
	public function getAdminList($eid){
		$memberList = $this->getMemberList($eid);
		$adminList = array();
		for($i=0; $i<count($memberList); $i++){
			$member =  $memberList[$i];
			if($member->isAdmin){
				$adminList[] = $member;
			}
		}
		
		return $adminList;
	}
	
	public function isEventMember($eid, $uid){
		$uid = (int)$uid;
		
		if(!$uid) return false;
		$memberList = $this->getMemberList($eid);
		
		if(!empty($memberList)){
			foreach ($memberList as $member){
				if($member->uid == $uid)
					return true;
			}
		}
		return false;
	}
	
	public function isEventAdmin($eid, $uid){
		
		$uid = (int)$uid;
		$eid = (int)$eid;
		if(!$eid) return false;
		
		static $s_admins;
		if(isset($s_admins[$eid]) && isset($s_admins[$eid][$uid]))
			return $s_admins[$eid][$uid];
		if(!$uid) {
			$s_admins[$eid][$uid] = false;
			return false;
		}
		$ctrl = $this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
		//if($ctrl->isAdmin()) {
		
		if($ctrl->hasPerm(EVENT_PERM_ADMIN)){
			$s_admins[$eid][$uid] = true;
			return true;
		}
		
		$memberList = $this->getMemberList($eid);
		
		if(!empty($memberList)){
			foreach ($memberList as $member){
				if($member->uid == $uid && $member->isAdmin){
					$s_admins[$eid][$uid] = true;
					return true;
				}
					
			}
		}
		$s_admins[$eid][$uid] = false;
		return false;
	}
	
	public function getTermInfo($tid, $use_cached=true)
    {
    	static $terms_info;
    	if(empty($terms_info))
    		$terms_info = array();
    	if(!empty($terms_info) && isset($terms_info[$tid]) && !empty($terms_info[$tid])){
    		return $terms_info[$tid];
    	}
    	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
		$cache_key="term_info";
		
		if ($use_cached){
			$terms_info=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
		}
    	if(!empty($terms_info) && isset($terms_info[$tid]) && !empty($terms_info[$tid])){
    		return $terms_info[$tid];
    	}
    	
		$termModel = $ctrl->loadModel("Term");
		
		$term = $termModel->get($tid);
		if ($use_cached && $term)
		{
			$terms_info[$tid] = $term;
			$ctrl->setCache($cache_key, $terms_info, EVENT_CACHE_PREFIX);
		}
		
		return $term;
	}
	
	
	// Get Vote of Event
	public function eventVoteInfo($eid, $thid=0, $pid=0, $type=0, $userCache=true, $static=true){
		static $evote_info_all;
		if(!is_array($evote_info_all) || !isset($evote_info_all[$type]))
			$evote_info_all[$type] = array();
		$evote_info = $evote_info_all[$type]; 
		
		if($static){
			if(is_numeric($thid) && $thid>0 &&is_numeric($pid) && $pid>0)
				if(isset($evote_info[$eid][$thid][$pid]))
					return $evote_info[$eid][$thid][$pid];
			if(is_numeric($thid) && $thid>0){
				if(isset($evote_info[$eid][$thid]))
					return $evote_info[$eid][$thid];
			}
			else{
				if(isset($evote_info[$eid]))
					return $evote_info[$eid];
			}
		}
		else{
			unset($evote_info_all[$type][$eid]);
		}
	
		$ctrl=$this->_actionController;
		if(!$ctrl)
			$ctrl = $this->_ctrl;
		$cache_key="event_vote_info_$eid";
		
		$ret = false;
		if ($userCache){
			$ret=$ctrl->getCache($cache_key, EVENT_CACHE_PREFIX);
			if ($ret !== false){
				$evote_info_all[$type][$eid] = $ret;
			}
		}
		if($ret === false){
			//$voteModel = $ctrl->loadModel('Vote');
			$voteModel = AppGlobal::loadModel("Vote", 'event');
			$ret = $voteModel->getEventVotes($eid, $type);
			$evote_info_all[$type][$eid] = $ret;
		}
		
		if(is_numeric($thid) && $thid>0 &&is_numeric($pid) && $pid>0){
			//$evote_info[$eid][$thid][$pid] = $ret[$thid][$pid];
			return $ret[$thid][$pid];
		}
		if(is_numeric($thid) && $thid>0){
			//$evote_info[$eid][$thid] = $ret[$thid];
			return $ret[$thid];
		}
		
		return $ret;
		
	}
	// Get thread list of Event
	public function eventThreadList($eid, $thid=0, $pid=0, $type=1){
		$vote_info = $this->eventVoteInfo($eid, $thid, $pid, $type);
		$ret = array();
		if(is_array($vote_info) && count($vote_info) > 0)
			foreach($vote_info as $thread){
				if(is_array($thread) && count($thread)>0){
					foreach($thread as $phase){
						$ret[] = $phase;
						break;
					}
				}
			}
		return $ret;
	}
	
}


